package com.project.schoolFoodShare.controller.restaurant;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.schoolFoodShare.common.enums.BusinessType;
import com.project.schoolFoodShare.common.enums.ResultCode;
import com.project.schoolFoodShare.domain.FoodList;
import com.project.schoolFoodShare.domain.FoodRestaurant;
import com.project.schoolFoodShare.domain.FoodSchool;
import com.project.schoolFoodShare.domain.Result;
import com.project.schoolFoodShare.service.FoodListService;
import com.project.schoolFoodShare.service.FoodRestaurantService;
import com.project.schoolFoodShare.service.FoodSchoolService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @description: 餐厅controller
 */
@Controller
@ResponseBody
@RequestMapping("restaurant")
public class FoodRestaurantController {

    @Autowired
    private FoodRestaurantService foodRestaurantService;
    @Autowired
    private FoodSchoolService foodSchoolService;
    @Autowired
    private FoodListService foodListService;

    /** 分页获取餐厅 */
    @PostMapping("getFoodRestaurantPage")
    public Result getFoodRestaurantPage(@RequestBody FoodRestaurant foodRestaurant) {
        Page<FoodRestaurant> page = new Page<>(foodRestaurant.getPageNumber(),foodRestaurant.getPageSize());
        QueryWrapper<FoodRestaurant> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(foodRestaurant.getSchool()),FoodRestaurant::getSchool,foodRestaurant.getSchool())
                .like(StringUtils.isNotBlank(foodRestaurant.getName()),FoodRestaurant::getName,foodRestaurant.getName())
                .like(StringUtils.isNotBlank(foodRestaurant.getAddress()),FoodRestaurant::getAddress,foodRestaurant.getAddress());
        Page<FoodRestaurant> foodRestaurantPage = foodRestaurantService.page(page, queryWrapper);
        return Result.success(foodRestaurantPage);
    }

    @GetMapping("getFoodRestaurantBySchool")
    public Result getFoodRestaurantBySchool(@RequestParam("school")String school) {
        FoodSchool foodSchool = foodSchoolService.getById(school);
        QueryWrapper<FoodRestaurant> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FoodRestaurant::getSchool,foodSchool.getName());
        List<FoodRestaurant> restaurantList = foodRestaurantService.list(wrapper);
        return Result.success(restaurantList);
    }

    @GetMapping("getFoodRestaurantList")
    public Result getFoodRestaurantList() {
        List<FoodRestaurant> list = foodRestaurantService.list();
        return Result.success(list);
    }

    /** 根据id获取餐厅 */
    @GetMapping("getFoodRestaurantById")
    public Result getFoodRestaurantById(@RequestParam("id")String id) {
        FoodRestaurant foodRestaurant = foodRestaurantService.getById(id);
        return Result.success(foodRestaurant);
    }

    /** 保存餐厅 */
    @PostMapping("saveFoodRestaurant")
    public Result saveFoodRestaurant(@RequestBody FoodRestaurant foodRestaurant) {
        boolean save = foodRestaurantService.save(foodRestaurant);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑餐厅 */
    @PostMapping("editFoodRestaurant")
    public Result editFoodRestaurant(@RequestBody FoodRestaurant foodRestaurant) {
        boolean save = foodRestaurantService.updateById(foodRestaurant);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除餐厅 */
    @GetMapping("removeFoodRestaurant")
    public Result removeFoodRestaurant(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                QueryWrapper<FoodList> wrapper = new QueryWrapper<>();
                wrapper.lambda().eq(FoodList::getRestId,id);
                int count = foodListService.count(wrapper);
                if (count > 0) {
                    return Result.fail("当前餐厅存在美食分享,无法删除");
                }
                foodRestaurantService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("餐厅id不能为空！");
        }
    }

}
